<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form :inline="true" :model="queryParams">
      <el-form-item label="名称">
        <el-input v-model="queryParams.productname" placeholder="商品名称" size="small"></el-input>
      </el-form-item>
      <el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="fetchData">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form-item>
    </el-form>

    <!-- 操作 -->
    <el-row :gutter="10" class="toolbar">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column prop="name" label="名称"></el-table-column>
      <el-table-column prop="img1" label="图一">
        <template slot-scope="scope">
          <el-image class="list-img" :src="scope.row.img1" :fit="contain"/>
        </template>
      </el-table-column>
      <el-table-column prop="img2" label="图二">
        <template slot-scope="scope">
          <el-image class="list-img" :src="scope.row.img2" :fit="contain"/>
        </template>
      </el-table-column>
      <el-table-column prop="taoword" label="淘宝口令"></el-table-column>
      <el-table-column prop="endTime" label="结束时间">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-copy-document"
            @click="handleCopy(scope.row)"
          >分享
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--          >修改-->
          <!--          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <!-- 分页组件 -->
    <el-pagination
      class="page"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 修改弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
      <el-form :model="dialogForm">
        <el-form-item label="名称" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.name" placeholder="请输入商品名" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="淘口令" :label-width="formLabelWidth">
          <el-input v-model="dialogForm.taoword" placeholder="请输入淘口令" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="有效期至" :label-width="formLabelWidth">
          <el-date-picker
            v-model="dialogForm.endTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="图一" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :http-request="uploadFile1"
          >
            <img v-if="dialogForm.img1" :src="dialogForm.img1" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="图二" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :http-request="uploadFile2"
          >
            <img v-if="dialogForm.img2" :src="dialogForm.img2" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, upload, addProduct, delProduct } from '@/api/product'
import { Message } from 'element-ui'

export default {
  data() {
    return {
      list: null,
      listLoading: true,
      dialogTitle: '',
      dialogFormVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productname: null,
        startTime: null,
        endTime: null
      },
      // 表单参数
      dialogForm: {},
      dialogFormReset: {
        name: null,
        taoword: null,
        endTime: null,
        img1: null,
        img2: null
      },
      formLabelWidth: '120px',
      total: 0 // 总条目数
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    resetQuery() {
      this.queryParams.pageNum = 1
      this.fetchData()
    },
    fetchData() {
      this.listLoading = true
      getList(this.queryParams).then(response => {
        this.list = response.data
        this.total = +response.totalElement
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.fetchData()
    },
    handleAdd() {
      this.dialogTitle = '新增'
      this.dialogForm = Object.assign({}, this.dialogFormReset)
      this.dialogFormVisible = true
    },
    handleUpdate(d) {
      this.dialogTitle = '修改'
      this.dialogForm = Object.assign({}, this.dialogFormReset, d)
      this.dialogFormVisible = true
    },
    dialogConfirm() {
      if (!this.dialogForm.id) {
        // 新增
        return addProduct(this.dialogForm).then(_ => {
          this.dialogFormVisible = false
          this.fetchData()
        })
      }
      // 修改
    },
    handleDelete(d) {
      delProduct(d.id).then(_ => this.fetchData())
    },
    handleCopy(d) {
      let webhost = process.env.VUE_APP_WEB_DOMAIN
      if (!webhost) {
        webhost = `http://localhost:${process.env.port || process.env.npm_config_port || 9528}`
      }
      const content = `${d.name}\n${webhost}/detail?id=${d.id}`
      //   navigator.clipboard.writeText(content).then(() => {
      //     this.$message.success('已经复制分享链接')
      //   })
      const input = document.createElement('textarea')
      input.value = content // 替换为你要复制的内容
      document.body.appendChild(input)
      input.select()
      document.execCommand('copy')
      document.body.removeChild(input)
      this.$message.success('已经复制分享链接')
    },
    beforeUpload(rawFile) {
      // 这里设置上传图片类型和限制大小
      if (rawFile.type !== 'image/jpeg' && rawFile.type !== 'image/png') {
        Message.error('图片必须是jpg或png格式')
        return false
      }
      return true
    },
    uploadFile1(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      // const loading = this.$loading({
      //   lock: true,
      //   text: 'Loading',
      //   spinner: 'el-icon-loading',
      //   background: 'rgba(0, 0, 0, 0.7)'
      // })
      upload(formData).then(res => {
        this.dialogForm.img1 = res.data
        // loading.close()
      })
    },
    uploadFile2(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      upload(formData).then(res => {
        this.dialogForm.img2 = res.data
      })
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.list-img {
  height: 100px;
}

.toolbar {
  margin: 20px;
}

.page {
  margin: 20px;
}
</style>
